package com.xuecheng.manage_course.controller;

import com.xuecheng.framework.domain.course.ext.CourseInfo;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.framework.domain.course.request.CourseListRequest;
import com.xuecheng.framework.domain.course.response.CoursePublishResult;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.utils.XcOauth2Util;
import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_course.entity.*;
import com.xuecheng.manage_course.mapper.TeachplanMapper;
import com.xuecheng.manage_course.service.impl.CourseBaseServiceImpl;
import com.xuecheng.manage_course.service.impl.CourseMarketServiceImpl;
import com.xuecheng.manage_course.service.impl.CoursePicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/manage_course/course-base")
public class CourseBaseController extends BaseController {

    @Autowired
    CourseBaseServiceImpl courseBaseService;

    @Autowired
    CoursePicServiceImpl coursePicService;

    @Autowired
    CourseMarketServiceImpl courseMarketService;

    @Autowired
    TeachplanMapper teachplanMapper;

    @GetMapping("/getCourseView")
    public CourseView getCourseView(String id) {
        CourseView courseView = new CourseView();

        //查询课程基本信息
        CourseBase courseBase = courseBaseService.getById(id);
        if (courseBase != null) {
            courseView.setCourseBase(courseBase);
        }

        //查询课程图片
        CoursePic coursePic = coursePicService.getById(id);
        if (coursePic != null) {
            courseView.setCoursePic(coursePic);
        }

        //查询课程营销信息
        CourseMarket courseMarket = courseMarketService.getById(id);
        if (courseMarket != null) {
            courseView.setCourseMarket(courseMarket);
        }

        //课程计划信息
        TeachplanNode teachplanNode = teachplanMapper.selectList1(id);
        courseView.setTeachplanNode(teachplanNode);

        return courseView;
    }

    @PostMapping("/preview/{id}")
    public CoursePublishResult preview(@PathVariable("id") String id) {
        return courseBaseService.preview(id);
    }


    @GetMapping("/coursebase/list/{page}/{size}")
    public QueryResponseResult<CourseInfo> findCourseList(@PathVariable("page") int page,
                                                          @PathVariable("size") int size,
                                                          CourseListRequest courseListRequest) {
        //获取当前用户信息
        XcOauth2Util xcOauth2Util = new XcOauth2Util();
        XcOauth2Util.UserJwt userJwt = xcOauth2Util.getUserJwtFromHeader(request);
        //当前用户所属单位的id
        String company_id = userJwt.getCompanyId();
        QueryResponseResult<CourseInfo> queryResponseResult = courseBaseService.findCourseList(company_id,page,size,courseListRequest);
        return queryResponseResult;
    }

}

